package edu.usc.epigenome.dnaase.script;
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import net.sf.samtools.SAMFileHeader;
import net.sf.samtools.SAMFileReader;
import net.sf.samtools.SAMReadGroupRecord;
import net.sf.samtools.SAMRecord;
import net.sf.samtools.util.CloseableIterator;

import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;




public class SamAddReadGroupHeader {
	/**
	 * @param args
	 */
		
		final private static String USAGE = "SamAddReadGroupHeader [opts] file1.bam file2.bam ...";



		
		/**
		 * object vars
		 */

		
		/**
		 * @param args
		 */
		@Option(name="-chrom",multiValued=true,usage="One or more chroms, eg. --chrom chr1 --chrom chr5")
		protected List<String> chrs = new ArrayList<String>(25);
		@Option(name="-readGroupHeaderFile",usage="readGroupHeaderFile")
		protected String readGroupHeaderFile = null;
		@Option(name="-debug",usage=" Debugging statements (default false)")
		protected boolean debug = false;

		
		// receives other command line parameters than options
		@Argument
		private List<String> stringArgs = new ArrayList<String>();

		
		
		/**
		 * @param args
		 */
		public static void main(String[] args)
		throws Exception
		{
			new SamAddReadGroupHeader().doMain(args);
		}

		public void doMain(String[] args)
		throws Exception {

			CmdLineParser parser = new CmdLineParser(this);
			// if you have a wider console, you could increase the value;
			// here 80 is also the default
			parser.setUsageWidth(80);
			try
			{
				parser.parseArgument(args);
				if (stringArgs.size() < 1) throw new CmdLineException(USAGE);
				
			}
			catch (CmdLineException e)
			{
				System.err.println(e.getMessage());
				parser.printUsage(System.err);
				System.err.println();
				return;
			}
			
			for (final String chr : chrs)
			{

				for (final String fn : stringArgs)
				{
					File inputSamOrBamFile = new File(fn);

					final SAMFileReader inputSam = new SAMFileReader(inputSamOrBamFile);
					inputSam.setValidationStringency(SAMFileReader.ValidationStringency.SILENT);
					
					SAMFileHeader samHeader = inputSam.getFileHeader();
					List<SAMReadGroupRecord> samReadGroups = samHeader.getReadGroups();
					System.out.println(samReadGroups.size());
					for (String comment : samHeader.getComments()){
						System.out.println(comment);
					}
					System.out.println(samHeader.getTextHeader());
					//samHeader.setTextHeader("@RG	ID:UEC2011:1	PL:ILLUMINA	CN:UEC");
					System.out.println(samHeader.getTextHeader());
					//for (SAMReadGroupRecord samReadGroup : samReadGroups){
					//	System.out.println(samReadGroup.getDescription());
					//}
					
					CloseableIterator<SAMRecord> chrIt = inputSam.query(chr, 0, 0, false);
					record: while (chrIt.hasNext())
					{
						SAMRecord samRecord = chrIt.next();
						SAMReadGroupRecord samReadGroup1 = samRecord.getReadGroup();
						//samReadGroup1.READ_GROUP_ID_TAG = "UEC2011:1";
						System.out.println(samReadGroup1.getId()  );
					
					}
				}
			}
		}
}
